<?php
//获取SMTP服务器信息
function getSmtp()
{
    $list = M('sys_config')->field('cfg_name,cfg_value')->where('id>=6 and id<=10')->select();
    foreach ($list as $v) {
        $smtp[$v['cfg_name']] = $v['cfg_value'];
    }
    return $smtp;
}
//发邮件
function actmail($email, $title, $str)
{
    Vendor('PHPMailer.PHPMailerAutoload');

    $email_info = getSmtp();

    $mail = new PHPMailer;

    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = $email_info['smtp_host'];  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = $email_info['smtp_user'];                 // SMTP username
    $mail->Password = $email_info['smtp_pw'];                           // SMTP password
    $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = $email_info['smtp_port'];                                    // TCP port to connect to
    $mail->CharSet='UTF-8';                               // Set Characters

    $mail->setFrom($email_info['smtp_user'], $email_info['smtp_name']);
    $mail->addAddress($email, $email);     // Add a recipient
    $mail->isHTML(true);                                  // Set email format to HTML

    $mail->Subject = $title;
    $mail->Body    = $str;

    if (!$mail->send()) {
        return ['error'=>'发送失败','errorInfo'=>$mail->ErrorInfo];
    } else {
        return ['info'=>'发送完毕'];
    }
}

//获取用户信息
function UserInfo($get_uid)
{
    return D('users')->relation(true)->where('uid="'.$get_uid.'"')->find();
}

//返回随机字符串
function getRandStr($length)
{
    $str = '';
    while (strlen($str) < $length) {
        $chr_num = mt_rand(48, 122);
        if (($chr_num >= 48 && $chr_num <= 57) || ($chr_num >= 65 && $chr_num <= 90) ||($chr_num >= 97 && $chr_num <= 122)) {
            $str .= chr($chr_num);
        }
    }
    return $str;
}

//格式化时间
function timeFormat($time)
{
    return date('Y-m-d H:i:m', $time);
}

//获取用户IP
function getClientIP()
{
    global $ip;
    if (getenv("HTTP_CLIENT_IP")) {
        $ip = getenv("HTTP_CLIENT_IP");
    } elseif (getenv("HTTP_X_FORWARDED_FOR")) {
        $ip = getenv("HTTP_X_FORWARDED_FOR");
    } elseif (getenv("REMOTE_ADDR")) {
        $ip = getenv("REMOTE_ADDR");
    } else {
        $ip = "Unknow";
    }
    return $ip;
}
